<template>
  <el-alert :title="title" :type="type" :description="message" :closable="false" show-icon>
  </el-alert>
</template>

<script>
  export default {
    name: 'authredirect',
    data() {
      return {
        title: '自动登录中',
        type: 'info',
        message: ''
      }
    },
    created() {
      if (!window.DingTalkPC) {
        this.message = '钉钉脚本未加载'
        return
      }
      if(!DingTalkPC.ua.isInDingTalk){
        this.message = '请在钉钉PC版客户端打开此链接！'
        return
      }
      let that = this
      DingTalkPC.config(__config)
      DingTalkPC.ready(function (res) {
        DingTalkPC.runtime.permission.requestAuthCode({
          corpId: __config.corpId, //企业ID
          onSuccess: async function (result) {
            that.message = `获取授权码成功！`
            let ret = await that.$store.dispatch('loginDD', result)
            if(ret.success){
              that.$router.push({ path: '/' })
            }
          },
          onFail: function (err) {
            that.message = `获取授权码失败！${JSON.stringify(err)}`
          }
        })
      })
      DingTalkPC.error(function (error) {
        that.message = $`${error.errorCode},错误信息：${error.errorMessage}`
        /*{
            errorCode: 1001, //错误码
            errorMessage: '', //错误信息
        }*/
        // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开调试窗口的 console查看，也可以在返回的res参数中查看。
      });
    }
  }
</script>
